package LeetCode.leetcode;

/**
 * @author: leiwenkang02@meituan.com
 * @date: 2018/8/2
 * @time: 21:07
 */
public class Test38外观数列 {
    public static void main(String[] args) {
        System.out.println(countAndSay(5));
    }
    public static String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }
        if (n == 2) {
            return "11";
        }
        String lastOne = countAndSay(n - 1);
        char[] ch = lastOne.toCharArray();
        int index = 0;
        StringBuilder sb = new StringBuilder();
        int count = 1;
        for (int i = 1; i <= ch.length; i++) {
            if (i == ch.length || ch[i] != ch[index]) {
                sb.append(count);
                sb.append(ch[index]);
                index = i;
                count = 1;
            } else {
                count++;
            }
        }
        return sb.toString();
    }
}
